/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.jiujiudevice.service;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.boxin.ims.modules.jiujiudevice.dao.JiuDeviceDao;
import com.boxin.ims.modules.jiujiudevice.entity.JiuDevice;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;

/**
 * 设备配置Service
 * @author sxh
 * @version 2018-08-15
 */
@Component
@Transactional(readOnly = true)
public class JiuDeviceService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(JiuDeviceService.class);
	
	@Autowired
	private JiuDeviceDao jiuDeviceDao;
	
	public JiuDevice get(Long id) {
		return jiuDeviceDao.findOne(id);
	}
	/**
	 * 根据sn 获取一个设备信息
	 * @param sn
	 * @return
	 */
	public JiuDevice getBySn(String sn) {
		DetachedCriteria dc = jiuDeviceDao.createDetachedCriteria();
		dc.add(Restrictions.eq("sn", sn));
		List<JiuDevice> list = jiuDeviceDao.find(dc);
		if(list.size() > 0) {
			return list.get(0);
		}else {
			return null;
		}
	}
	
	public Page<JiuDevice> find(Page<JiuDevice> page, JiuDevice jiuDevice) {
		DetachedCriteria dc = jiuDeviceDao.createDetachedCriteria();
		if (jiuDevice.getId()!=null && jiuDevice.getId()>0){
			dc.add(Restrictions.eq("id", jiuDevice.getId()));
		}
		if (StringUtils.isNotEmpty(jiuDevice.getName())){
			dc.add(Restrictions.like("name", "%"+jiuDevice.getName()+"%"));
		}
		if (StringUtils.isNotEmpty(jiuDevice.getSn())){
			dc.add(Restrictions.eq("sn", jiuDevice.getSn()));
		}
		dc.addOrder(Order.desc("id"));
		return jiuDeviceDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public void save(JiuDevice jiuDevice) {
		jiuDeviceDao.save(jiuDevice);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		jiuDeviceDao.deleteById(id);
	}
	
	//得到所有的设备
	public List<JiuDevice> findAll() {
		// TODO Auto-generated method stub
		DetachedCriteria dc = jiuDeviceDao.createDetachedCriteria();
		return jiuDeviceDao.find(dc);
	}
	
}
